//
//  ChartItem.m
//  CoolMove
//
//  Created by CA on 14-9-15.
//  Copyright (c) 2014年 CA. All rights reserved.
//

#import "ChartItem.h"

@interface ChartItem ()
@property (nonatomic, assign)CGFloat forecastSpeed;//预测球速
@end

@implementation ChartItem

- (instancetype)init
{
    self = [super init];
    if (self) {
        _poseType = SportPoseTypeFaQiu;
        _maxSpeed = @"0";
        _strength = @"0";
        _arc = @"0";
        _sportStartTime = @"";
        _sportStopTime = @"";
        _indexNum = @"";
        _unixTimeStamp = @"0";
        _forecastSpeed = 0;
    }
    return self;
}

- (NSString *)poseTypeDescription
{
    switch (self.poseType) {
        case SportPoseTypePingJi:
            return NSLocalizedString(@"Flat", nil);
            break;
        case SportPoseTypeXuanQiu:
            return NSLocalizedString(@"Topspin", nil);
            break;
        case SportPoseTypeXueQiu:
            return NSLocalizedString(@"Slice", nil);
            break;
        case SportPoseTypeJieJi:
            return NSLocalizedString(@"Volley", nil);
            break;
        case SportPoseTypeKouSha:
            return NSLocalizedString(@"Smash", nil);
            break;
        case SportPoseTypeFaQiu:
            return NSLocalizedString(@"Serve", nil);
            break;
        default:
            return @"空挥";
            break;
    }
}

- (void)parseChartItemDictionary:(NSDictionary *)dictionary
{
    NSString *poseStr = dictionary[@"Batting"];
    self.poseType = [self poseTypeByPoseStr:poseStr];
    self.maxSpeed = dictionary[@"Speed"];
    self.strength = dictionary[@"Force"];
    self.arc = dictionary[@"Radian"];
    self.sportStartTime = dictionary[@"SportStartTime"];
    self.sportStopTime = dictionary[@"SportStopTime"];
    self.indexNum = dictionary[@"Num"];
}

- (SportPoseType)poseTypeByPoseStr:(NSString *)poseStr
{
    if ([poseStr isEqualToString:@"type1"]) {
        return SportPoseTypePingJi;
    } else if ([poseStr isEqualToString:@"type2"]) {
        return SportPoseTypeXuanQiu;
    } else if ([poseStr isEqualToString:@"type3"]) {
        return SportPoseTypeXueQiu;
    } else if ([poseStr isEqualToString:@"type4"]) {
        return SportPoseTypeJieJi;
    } else {
        return SportPoseTypeKouSha;
    }
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
    if (self) {
        _poseType = [aDecoder decodeIntegerForKey:@"PoseType"];
        _maxSpeed = [aDecoder decodeObjectForKey:@"MaxSpeed"];
        _forecastSpeed = [aDecoder decodeFloatForKey:@"ForecastSpeed"];
        _strength = [aDecoder decodeObjectForKey:@"Strength"];
        _arc = [aDecoder decodeObjectForKey:@"Arc"];
        _unixTimeStamp = [aDecoder decodeObjectForKey:@"UnixTimeStamp"];
        _sportStartTime = [aDecoder decodeObjectForKey:@"SportStartTime"];
        _sportStopTime = [aDecoder decodeObjectForKey:@"SportStopTime"];
        _indexNum = [aDecoder decodeObjectForKey:@"IndexNum"];
        _handDirectionType = [aDecoder decodeIntegerForKey:@"HandDirectionType"];
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeInteger:self.poseType forKey:@"PoseType"];
    [aCoder encodeObject:self.maxSpeed forKey:@"MaxSpeed"];
    [aCoder encodeFloat:self.forecastSpeed forKey:@"ForecastSpeed"];
    [aCoder encodeObject:self.strength forKey:@"Strength"];
    [aCoder encodeObject:self.arc forKey:@"Arc"];
    [aCoder encodeObject:self.unixTimeStamp forKey:@"UnixTimeStamp"];
    [aCoder encodeObject:self.sportStartTime forKey:@"SportStartTime"];
    [aCoder encodeObject:self.sportStopTime forKey:@"SportStopTime"];
    [aCoder encodeObject:self.indexNum forKey:@"IndexNum"];
    [aCoder encodeInteger:self.handDirectionType forKey:@"HandDirectionType"];
}

#pragma mark - getter & setter
- (void)setMaxSpeed:(NSString *)maxSpeed {
    _maxSpeed = maxSpeed;
    
    CGFloat forecastSpeed = [maxSpeed floatValue];
    if (forecastSpeed < 110) {
        forecastSpeed = forecastSpeed * ((arc4random_uniform(10) + 116)/ 100.0);
    }else if(forecastSpeed < 125 && forecastSpeed >= 110){
        forecastSpeed = forecastSpeed * ((arc4random_uniform(8) + 121)/ 100.0);
    }else if (forecastSpeed >= 125){
        forecastSpeed = forecastSpeed * ((arc4random_uniform(10) + 116)/ 100.0);
    }
    
    self.forecastSpeed = (NSInteger)roundf(forecastSpeed) * 1.0;

}


#pragma mark - Public Method

- (NSString *)power
{
    NSUInteger power = 0;
    if ([self.arc integerValue] <= 14) {
        power = [self.strength integerValue] * 200/14;
    } else if ([self.arc integerValue] < 200) {
        power = [self.strength integerValue] * 200/[self.arc integerValue];
    } else {
        power = [self.strength integerValue] * 0.5;
    }
    
    return [NSString stringWithFormat:@"%zd", power];
}

- (NSString *)killability
{
    return [NSString stringWithFormat:@"%zd", [self.maxSpeed integerValue]*800/350];
}

- (CGFloat)forecastSpeed {
    
    if (_forecastSpeed <= 0) {
        CGFloat forecastSpeed = [self.maxSpeed floatValue];
        if (forecastSpeed < 110) {
            forecastSpeed = forecastSpeed * ((arc4random_uniform(10) + 116)/ 100.0);
        }else if(forecastSpeed < 125 && forecastSpeed >= 110){
            forecastSpeed = forecastSpeed * ((arc4random_uniform(8) + 121)/ 100.0);
        }else if (forecastSpeed >= 125){
            forecastSpeed = forecastSpeed * ((arc4random_uniform(10) + 116)/ 100.0);
        }
    }

    return _forecastSpeed;
}



@end
